package RECUR_95;

import java.util.LinkedList;
import java.util.List;

import LCommon.TreeNode;

public class Solution {
	public List<TreeNode> generateTrees(int n) {
		if (n == 0) {
			return new LinkedList<TreeNode>();
		}
		return generate_trees(1, n);
	}

	private LinkedList<TreeNode> generate_trees(int start, int end) {
		LinkedList<TreeNode> all_tree = new LinkedList<TreeNode>();
		if (start > end) {
			all_tree.add(null);
			return all_tree;
		}
		for (int i = start; i <= end; i++) {
			LinkedList<TreeNode> left = generate_trees(start, i - 1);
			LinkedList<TreeNode> right = generate_trees(i+1, end);
			for (TreeNode l : left) {
				for (TreeNode r : right) {
					TreeNode currNode =new TreeNode(i);
					currNode.left=l;
					currNode.right=r;
					all_tree.add(currNode);
				}
			}
		}

		return all_tree;
	}
}
